/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VennDiagramMain.java
 *
 * Created on Nov 13, 2011, 3:35:37 PM
 */
package venndiagram;

import java.awt.Color;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author beckern
 */
public class VennDiagramMain extends javax.swing.JFrame {
    
    private boolean firstSetNameClicked = false;    //Text field for first set name has been clicked at least once
    private boolean secondSetNameClicked = false;   //Text field for second set name has been clicked at least once
    private boolean thirdSetNameClicked = false;    //Text field for third set name has been clicked at least once
    
    //Create file chooser
    final JFileChooser fileChooser = new JFileChooser();

    /** Creates new form VennDiagramMain */
    public VennDiagramMain() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        numberOfFilesLabel = new javax.swing.JLabel();
        numberOfLists = new javax.swing.JComboBox();
        firstFileLabel = new javax.swing.JLabel();
        firstFileText = new javax.swing.JTextField();
        firstBrowseButton = new javax.swing.JButton();
        secondFileLabel = new javax.swing.JLabel();
        secondFileText = new javax.swing.JTextField();
        secondBrowseButton = new javax.swing.JButton();
        thirdFileLabel = new javax.swing.JLabel();
        thirdFileText = new javax.swing.JTextField();
        thirdBrowseButton = new javax.swing.JButton();
        generateButton = new javax.swing.JButton();
        firstSetNameField = new javax.swing.JTextField();
        secondSetNameField = new javax.swing.JTextField();
        thirdSetNameField = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        fileDropDown = new javax.swing.JMenu();
        saveReportMenu = new javax.swing.JMenuItem();
        exitMenuOption = new javax.swing.JMenuItem();
        editDropDown = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));
        setName("mainFrame"); // NOI18N
        setResizable(false);

        mainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Gene Comparison Tool", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Sans Light", 0, 24), new java.awt.Color(27, 27, 27))); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(776, 549));
        mainPanel.setRequestFocusEnabled(false);

        numberOfFilesLabel.setText("Select Number of Lists:");

        numberOfLists.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3" }));

        firstFileLabel.setText("Upload First File:");

        firstBrowseButton.setText("Browse");
        firstBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                firstBrowseButtonActionPerformed(evt);
            }
        });

        secondFileLabel.setText("Upload Second File:");

        secondBrowseButton.setText("Browse");
        secondBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                secondBrowseButtonActionPerformed(evt);
            }
        });

        thirdFileLabel.setText("Upload Third File:");

        thirdBrowseButton.setText("Browse");
        thirdBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thirdBrowseButtonActionPerformed(evt);
            }
        });

        generateButton.setFont(new java.awt.Font("DejaVu Serif", 0, 14));
        generateButton.setText("Generate");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        firstSetNameField.setForeground(new java.awt.Color(169, 165, 162));
        firstSetNameField.setText("Enter set name");
        firstSetNameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                firstSetNameFieldFocusGained(evt);
            }
        });

        secondSetNameField.setForeground(new java.awt.Color(169, 165, 162));
        secondSetNameField.setText("Enter set name");
        secondSetNameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                secondSetNameFieldFocusGained(evt);
            }
        });

        thirdSetNameField.setForeground(new java.awt.Color(169, 165, 162));
        thirdSetNameField.setText("Enter set name");
        thirdSetNameField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                thirdSetNameFieldFocusGained(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(numberOfFilesLabel)
                    .addComponent(firstFileLabel)
                    .addComponent(secondFileLabel)
                    .addComponent(thirdFileLabel))
                .addGap(45, 45, 45)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(numberOfLists, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(secondFileText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addComponent(firstFileText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addComponent(thirdFileText, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                            .addComponent(generateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(thirdBrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(secondBrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(firstBrowseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(thirdSetNameField)
                    .addComponent(secondSetNameField)
                    .addComponent(firstSetNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(numberOfFilesLabel)
                    .addComponent(numberOfLists, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstFileLabel)
                    .addComponent(firstFileText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstBrowseButton)
                    .addComponent(firstSetNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(secondFileLabel)
                    .addComponent(secondFileText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(secondBrowseButton)
                    .addComponent(secondSetNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(thirdFileText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(thirdFileLabel)
                    .addComponent(thirdBrowseButton)
                    .addComponent(thirdSetNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 284, Short.MAX_VALUE)
                .addComponent(generateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        fileDropDown.setText("File");

        saveReportMenu.setText("Save Report");
        fileDropDown.add(saveReportMenu);

        exitMenuOption.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        exitMenuOption.setText("Exit");
        exitMenuOption.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuOptionActionPerformed(evt);
            }
        });
        fileDropDown.add(exitMenuOption);

        menuBar.add(fileDropDown);

        editDropDown.setText("Edit");
        menuBar.add(editDropDown);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuOptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuOptionActionPerformed
        // Exit the program
        System.exit(0);
    }//GEN-LAST:event_exitMenuOptionActionPerformed

    //Adam Dalby
    //Open window for file selection, set textfield to chosen file location
    private void firstBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_firstBrowseButtonActionPerformed
        // TODO add your handling code here:
        
        //Set text field to file path if a file was selected
        int returnValue = fileChooser.showOpenDialog(this);
        if(returnValue == JFileChooser.APPROVE_OPTION)
        {
            File chosenFile = fileChooser.getSelectedFile();
            firstFileText.setText(chosenFile.getAbsolutePath());
        }

    }//GEN-LAST:event_firstBrowseButtonActionPerformed

    private void secondBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_secondBrowseButtonActionPerformed
        // TODO add your handling code here:
        
        //Set text field to file path if a file was selected
        int returnValue = fileChooser.showOpenDialog(this);
        if(returnValue == JFileChooser.APPROVE_OPTION)
        {
            File chosenFile = fileChooser.getSelectedFile();
            secondFileText.setText(chosenFile.getAbsolutePath());
        }
    }//GEN-LAST:event_secondBrowseButtonActionPerformed

    private void thirdBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thirdBrowseButtonActionPerformed
        // TODO add your handling code here:
        
        //Set text field to file path if a file was selected
        int returnValue = fileChooser.showOpenDialog(this);
        if(returnValue == JFileChooser.APPROVE_OPTION)
        {
            File chosenFile = fileChooser.getSelectedFile();
            thirdFileText.setText(chosenFile.getAbsolutePath());
        }
    }//GEN-LAST:event_thirdBrowseButtonActionPerformed

    
    //Generate comparison data 
    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        // TODO add your handling code here:
        
        //Construct an instance of CompareData with two Strings if two file paths were input
        if(firstFileText.getText().length() != 0 && secondFileText.getText().length() != 0 && thirdFileText.getText().length() == 0)
        {
            try
            {
                CompareData comparisonData = new CompareData(firstFileText.getText(), secondFileText.getText(), firstSetNameField.getText(), secondSetNameField.getText());
            }
            catch (Exception ex)
            {
                Logger.getLogger(VennDiagramMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        //Construct an instance of CompareData with three Strings if three file paths were input
        else if(firstFileText.getText().length() != 0 && secondFileText.getText().length() != 0 && thirdFileText.getText().length() != 0)
        {
            try
            {
                CompareData comparisonData = new CompareData(firstFileText.getText(), secondFileText.getText(), thirdFileText.getText(), firstSetNameField.getText(), secondSetNameField.getText(), thirdSetNameField.getText());
            }
            catch (Exception ex)
            {
                Logger.getLogger(VennDiagramMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        //Pop up error message if filepaths are not specified in first two textfields
        else
        {
            JOptionPane.showMessageDialog(this, "Select two or three files before clicking 'Generate'.", "Notice", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_generateButtonActionPerformed

    private void firstSetNameFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_firstSetNameFieldFocusGained
        // TODO add your handling code here:
        if(firstSetNameClicked == false)
        {
            firstSetNameField.setText("");
            firstSetNameField.setForeground(Color.black);
            firstSetNameClicked = true;
        }
    }//GEN-LAST:event_firstSetNameFieldFocusGained

    private void secondSetNameFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_secondSetNameFieldFocusGained
        // TODO add your handling code here:
        if(secondSetNameClicked == false)
        {
            secondSetNameField.setText("");
            secondSetNameField.setForeground(Color.black);
            secondSetNameClicked = true;
        }
    }//GEN-LAST:event_secondSetNameFieldFocusGained

    private void thirdSetNameFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_thirdSetNameFieldFocusGained
        // TODO add your handling code here:
        if(thirdSetNameClicked == false)
        {
            thirdSetNameField.setText("");
            thirdSetNameField.setForeground(Color.black);
            thirdSetNameClicked = true;
        } 
    }//GEN-LAST:event_thirdSetNameFieldFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VennDiagramMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VennDiagramMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VennDiagramMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VennDiagramMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new VennDiagramMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu editDropDown;
    private javax.swing.JMenuItem exitMenuOption;
    private javax.swing.JMenu fileDropDown;
    private javax.swing.JButton firstBrowseButton;
    private javax.swing.JLabel firstFileLabel;
    private javax.swing.JTextField firstFileText;
    private javax.swing.JTextField firstSetNameField;
    private javax.swing.JButton generateButton;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel numberOfFilesLabel;
    private javax.swing.JComboBox numberOfLists;
    private javax.swing.JMenuItem saveReportMenu;
    private javax.swing.JButton secondBrowseButton;
    private javax.swing.JLabel secondFileLabel;
    private javax.swing.JTextField secondFileText;
    private javax.swing.JTextField secondSetNameField;
    private javax.swing.JButton thirdBrowseButton;
    private javax.swing.JLabel thirdFileLabel;
    private javax.swing.JTextField thirdFileText;
    private javax.swing.JTextField thirdSetNameField;
    // End of variables declaration//GEN-END:variables
}
